package dev.spb3.controller;

import dev.spb3.model.Comment;
import jakarta.validation.Valid;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/comments")
public class CommentController {
    @GetMapping("/{movieId}")
    public ResponseEntity<List<Comment>> getCommentsByMovieId(@PathVariable Long movieId) {
        List<Comment> comments = Comment.getCommentsByMovieId(movieId);
        if (!comments.isEmpty()) {
            return ResponseEntity.ok(comments);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @PostMapping
    public ResponseEntity<?> addComment(@Valid @RequestBody Comment comment, BindingResult result) {
        if(result.hasErrors()) {
            return ResponseEntity.badRequest().body(result.getAllErrors().get(0).getDefaultMessage());
        }
        Comment.addComment(comment);
        return ResponseEntity.ok(comment);
    }
}
